package com.cwj.dao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import common.entity.common.person;
import common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Resource
    HttpServletRequest request;
    @Override
    public void insertFill(MetaObject metaObject) {

        Date date = new Date();
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.strictInsertFill(metaObject,"sign_time",LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"up_time",LocalDateTime.class,LocalDateTime.now());
        if(request.getAttribute("person")!=null){
            person person=(person)request.getAttribute("person");
            String id = common.entity.common.person.prefix + DateUtils.second_SimpleDateFormat.format(date) + person.getNickname();
            setFieldValByName("id",new Md5Hash(id).toHex(),metaObject);
            request.removeAttribute("person");
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
         }
}